package cn.iocoder.yudao.module.esim.controller.admin.esasset;

import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.module.esim.framework.config.ESimBusinessConfig;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.esim.controller.admin.esasset.vo.*;
import cn.iocoder.yudao.module.esim.dal.dataobject.esasset.EsAssetDO;
import cn.iocoder.yudao.module.esim.service.esasset.EsAssetService;
import cn.iocoder.yudao.module.esim.enums.ErrorCodeConstants;

@Tag(name = "管理后台 - eSim资产")
@RestController
@RequestMapping("/esim/es-asset")
@Validated
public class EsAssetController {

    @Resource
    private EsAssetService esAssetService;
    @Resource
    private ESimBusinessConfig eSimBusinessConfig;

    @PostMapping("/create")
    @Operation(summary = "创建eSim资产")
    @PreAuthorize("@ss.hasPermission('esim:es-asset:create')")
    public CommonResult<String> createEsAsset(@Valid @RequestBody EsAssetSaveReqVO createReqVO) {
        return success(esAssetService.createEsAsset(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新eSim资产")
    @PreAuthorize("@ss.hasPermission('esim:es-asset:update')")
    public CommonResult<Boolean> updateEsAsset(@Valid @RequestBody EsAssetSaveReqVO updateReqVO) {
        esAssetService.updateEsAsset(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除eSim资产")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('esim:es-asset:delete')")
    public CommonResult<Boolean> deleteEsAsset(@RequestParam("id") String id) {
        esAssetService.deleteEsAsset(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得eSim资产")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('esim:es-asset:query')")
    public CommonResult<EsAssetRespVO> getEsAsset(@RequestParam("id") String id) {
        EsAssetDO esAsset = esAssetService.getEsAsset(id);
        return success(BeanUtils.toBean(esAsset, EsAssetRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得eSim资产分页")
    @PreAuthorize("@ss.hasPermission('esim:es-asset:query')")
    public CommonResult<PageResult<EsAssetRespVO>> getEsAssetPage(@Valid EsAssetPageReqVO pageReqVO) {
        PageResult<EsAssetDO> pageResult = esAssetService.getEsAssetPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EsAssetRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出eSim资产 Excel")
    @PreAuthorize("@ss.hasPermission('esim:es-asset:export')")
    @OperateLog(type = EXPORT)
    public void exportEsAssetExcel(@Valid EsAssetPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EsAssetDO> list = esAssetService.getEsAssetPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "eSim资产.xls", "数据", EsAssetRespVO.class,
                        BeanUtils.toBean(list, EsAssetRespVO.class));
    }

}